package com.anymk.datastruture.array;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ArrayTest {
    public static void main(String[] args) {
        /*String[] strings = {"a", "b", "c", "d", "e"};
        System.out.println(strings.length);
        strings.clone();
        //_Dummy_.__Array__#clone
        //_Dummy_.__Array__#length
        Person[] p = new Person[3];
        Object o = new Object();
        System.out.println(p.getClass());
        //System.out.println(o.finalize());
        List<Person> list=new ArrayList<Person>();
        System.out.println(list.getClass());*/
        int[] abc = new int[]{1, 2, 3, 4};

        System.out.println("修改前的长度："+abc.length);
        resizeArray(abc,30);
        System.out.println("修改后的长度："+abc.length);
        for (int i : abc) {
            System.out.print("|"+i);
        }
        test07();

    }

    public static void resizeArray(int[] arr, int newLength) {
        Unsafe unsafe = getUnsafe();
        unsafe.getAndAddInt(arr,unsafe.addressSize()+4,newLength);
    }

    public static Unsafe getUnsafe() {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            return (Unsafe)field.get(null);

        } catch (Exception e) {
        }
        return null;
    }

    private static void test05() {
        Object[] o = new String[2];
        System.out.println(o.getClass().getName());
        System.out.println(o.getClass().getSuperclass().getName());
        String[] s = (String[]) o;
        System.out.println(s.getClass().getSuperclass().getName());
        Object oo = s;
    }

    private static void test06() {
        String[] s = new String[2];
        System.out.println(s.length);
        System.out.println(s.getClass().getDeclaredFields().length);  try {
            System.out.println(s.getClass().getDeclaredField("length"));
        } catch (NoSuchFieldException e) {
            System.out.println(e.toString());
        }




    }

    private static void test07() {
        //泛型不能协变
       /* List<Object> list = new ArrayList<String>();
        list.add(123);
        List<String> list1 = list;*/
        //String s = list1.get(0);    // 类型错误}
        Object[] o = new String[11];
        o[0]="1aaa";
        System.out.println(o.length);
    }

    public void test08(){
        Integer[] a=new Integer[11];
        System.out.println(a.length);
    }
}